<template>
  <div class="favorites-container">
    <!-- 返回按钮 -->
    <button @click="goBack" class="back-button">返回</button>

    <!-- 页面标题 -->
    <h2 class="page-title">我的收藏</h2>

    <!-- 收藏列表展示 -->
    <div class="favorites-list">
      <div
          v-for="(item, index) in stationInfo"
          :key="index"
          class="favorite-item"
      >
        <div class="favorite-info">
          <h3>{{ item.stationName}}</h3>
          <p>{{ item.address }}</p>
          <div class="original-price">
            {{ item.originalPrice }}/度
          </div>
        </div>

        <!-- 删除收藏按钮 -->
        <button @click="removeFavorite(index)" class="remove-btn">
          <span class="material-icons">查看车站详情</span>
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { GetStationList1 } from '@/api/station';
import { ref } from 'vue';
import router from '@/router';
const stationInfo = ref([]);
import { GetUserInfo } from '@/api/user';

const userId = ref(null); // 用于存储当前登录用户的 ID

// 获取用户信息并存储用户 ID
function getUserInfo() {
  GetUserInfo()
      .then(res => {
        if (res.data.code === 200000) {
          userId.value = res.data.data.id; // 获取用户 ID 并存储
          console.log("登录用户 ID:", userId.value);
          getStationInfo(); // 获取站点信息，传入用户 ID
        } else {
          console.error('获取用户信息失败:', res.data.message);
        }
      })
      .catch(err => {
        console.error("获取用户信息出错:", err);
      });
}

// 获取站点信息，并根据 userId 来查询数据
function getStationInfo() {
  if (!userId.value) {
    console.error('用户 ID 未获取');
    return;
  }

  GetStationList1({ userId: String(userId.value) })  // 将 userId 转换为字符串
      .then(res => {
        if (res.data.code === 200000) {
          stationInfo.value = res.data.data; // 存储站点信息
          console.log('获取站点列表成功:', stationInfo.value);
        } else {
          console.error('获取站点列表失败:', res.data.message);
        }
      })
      .catch(err => {
        console.error('获取站点列表出错:', err);
      });
}

// 初始调用获取用户信息
getUserInfo();

// 返回上一页
function goBack() {
  router.push("/mine/index");
}

// 删除收藏
function removeFavorite(index) {
  router.push('/home/index');

}
</script>

<style scoped>
/* 页面的整体布局 */
.favorites-container {
  padding: 20px;
  font-family: 'Arial', sans-serif;
  background-color: #f5f5f5;
  min-height: 100vh;
}

/* 页面标题样式 */
.page-title {
  font-size: 28px;
  color: #333;
  margin-bottom: 30px;
  text-align: center;
  font-weight: bold;
}

/* 返回按钮样式 */
.back-button {
  background-color: #4CAF50;
  color: white;
  padding: 12px 24px;
  border: none;
  cursor: pointer;
  border-radius: 8px;
  position: absolute;
  top: 20px;
  left: 20px;
  font-size: 16px;
  transition: background-color 0.3s ease;
}

.back-button:hover {
  background-color: #45a049;
}

/* 收藏列表容器 */
.favorites-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

/* 每个收藏项的卡片 */
.favorite-item {
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 20px;
  transition: transform 0.3s ease-in-out;
}

.favorite-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.15);
}

/* 收藏项标题和位置 */
.favorite-info h3 {
  margin: 0;
  font-size: 22px;
  color: #333;
  font-weight: bold;
}

.favorite-info p {
  margin: 8px 0 0;
  font-size: 14px;
  color: #777;
}

.original-price {
  color: #bbb;
  font-weight: bold;
  font-size: 14px;
  text-decoration: line-through;
}

/* 删除按钮样式 */
.remove-btn {
  background-color: #ff6f61;
  color: white;
  border: none;
  padding: 12px 20px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  transition: background-color 0.3s ease, transform 0.3s ease;
  margin-top: 10px;
  width: 100%;
}

.remove-btn:hover {
  background-color: #f44336;
  transform: scale(1.05);
}

.remove-btn .material-icons {
  font-size: 18px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .favorites-container {
    padding: 15px;
  }

  .page-title {
    font-size: 24px;
  }

  .favorites-list {
    grid-template-columns: 1fr;
  }

  .back-button {
    padding: 10px 18px;
    font-size: 14px;
  }
}
</style>
